/**
 * Created by hcadmin on 16/10/17.
 */
$(function(){

    //进入页面展示的数据

    var index = $.getUrlParam('index');
    //var keyword = $.getUrlParam('keyword');

    var localstorage = window.localStorage;

    var keyword = localstorage.getItem("keyword");

    var which_page="0";
    var search="";

    if (index == null){
        which_page = "0";
    }else {
        which_page = index;
    }

    //if (keyword == null){
    //    search = "";
    //}else {
    //    search = keyword;
    //}

    //根据页面传参加载对应列表
    if (keyword != null && keyword.length > 0){
        search = "1";
        if(which_page=="0"){
            openWaitContent();
        }else{
            openHaveContent();
        }
        $(".search").val(keyword);
        $(".search").addClass('searching');
        //搜索
        initSearch(keyword);

    }else {
        //加载默认页面
        if(which_page=="0"){
            showWaitApproval();
        }else{
            showHaveApproval();
        }
    }


    //if(which_page=="0"){
    //    showWaitApproval();
    //}else{
    //    showHaveApproval();
    //}
    //if(search.length > 0){
    //    initSearch(search);
    //}

    //点击"待我审批"
    $("#waitApproval").on('click',function(){
        which_page="0";
        showWaitApproval();
        //清空搜索框
        keyword = "";
        $("#search").val(keyword);
    });
    //点击"我已审批"
    $("#haveApproval").on('click',function(){
        which_page="1";
        showHaveApproval();
        //清空搜索框
        keyword = "";
        $("#search").val(keyword);
    });


    function showWaitApproval(){
        $(".tableview_cell1").remove();
        $("#blank_log").hide();
        //页面样式更改
        openWaitContent();
        //获取数据
        initData("waitContent",user_id,"0","1","0");
    }

    function showHaveApproval(){
        $(".tableview_cell1").remove();
        $("#blank_log").hide();
        //页面样式更改
        openHaveContent();
        //获取数据
        initData("haveContent",user_id,"1","1","0");
    }

    //点击行跳转
    $(document).on('click','.tableview_cell1',function(){
        var index;
        var approval_id = $(this).attr("id");

        var status      = $(this).attr("status");
        if(status==0){
            index="0";
        }else{
            index="1";
        }

        if ($(".search").val().length > 0){
            localstorage.setItem("keyword",$(".search").val());
        }

        window.location.href = "approvalDetail.html?index="+index+"&id="+approval_id+"&before_index="+ which_page +"";
    });


    //在搜索 框输入文字
    $("#search").bind("input propertychange",function(e){
        console.log(e);
        $(this).addClass('searching');
        var key = $(this).val();
            $(".tableview_cell1").remove();
            $("#blank_log").hide();
            search = "1";
            initSearch(key);
        if(key==""){
            $(this).removeClass('searching');
            localstorage.removeItem("keyword");
        }
        //debugger;
    });
    function initSearch(keyword){
        //var timestamp = $(".tableview_cell1").last().attr("time")==undefined?0:$(".tableview_cell1").last().attr("time");
        if(which_page=="0"){
            initData("waitContent",user_id,"0","1","0","1",keyword);
        }else{
            initData("haveContent",user_id,"1","1","0","1",keyword);
        }
    }

});
